<?php

/**
 *		//好友类//
 *		//这是一份试验品，由大街编写（zhongmingzi@gmail.com）
 *		//本程序默认环境：Zend Optimizer Yes / 3.3.3
 *		//		Apache/2.0.63 (Win32) PHP/5.2.12
 *		//		MySQL 支持 Yes / client lib version 5.0.89
 *		//		GD library Yes / bundled (2.0.34 compatible)
 *		//		eAccelerator/Memcache/xcache Yes
 *		//		cURL support enabled
 *		//时间：2011 3 25 08:45:23//
 */
if(!defined('IN')) {
	exit('Access Denied');
}

class friendmodel {

	function __construct() {
	}

	function add($uid, $friendid, $comment='') {
		$direction = DB::result_first("SELECT direction FROM ".DB::t('u_friends')." WHERE uid='$friendid' AND friendid='$uid' LIMIT 1");
		if($direction == 1) {
			DB::exec("INSERT INTO ".DB::t('u_friends')." SET uid='$uid', friendid='$friendid', comment='$comment', direction='3'", 'SILENT');
			DB::exec("UPDATE ".DB::t('u_friends')." SET direction='3' WHERE uid='$friendid' AND friendid='$uid'");
			return 1;
		} elseif($direction == 2) {
			return 1;
		} elseif($direction == 3) {
			return -1;
		} else {
			DB::exec("INSERT INTO ".DB::t('u_friends')." SET uid='$uid', friendid='$friendid', comment='$comment', direction='1'", 'SILENT');
			return DB::insert_id();
		}
	}

	function delete($uid, $friendids) {
		$friendids = dimplode($friendids, 0);
		$affectedrows = DB::exec("DELETE FROM ".DB::t('u_friends')." WHERE uid='$uid' AND friendid IN ($friendids)");
		if($affectedrows > 0) {
			DB::exec("UPDATE ".DB::t('u_friends')." SET direction=1 WHERE uid IN ($friendids) AND friendid='$uid' AND direction='3'");
		}
		return $affectedrows;
	}

	function get_totalnum_by_uid($uid, $direction = 0) {
		$sqladd = '';
		//修复
		if($direction == 0) {
			$sqladd = "uid='$uid' OR friendid='$uid'";
		} elseif($direction == 1) {
			$sqladd = "uid='$uid' ";
		} elseif($direction == 2) {
			$sqladd = "friendid='$uid' ";
		} elseif($direction == 3) {
			$sqladd = "uid='$uid' AND direction='3'";
		}
		$totalnum = DB::result_first("SELECT COUNT(*) FROM ".DB::t('u_friends')." WHERE $sqladd");
		return $totalnum;
	}

	function get_list($uid, $page, $limit, $direction = 0, $avatar = 0) {
		$count = self::get_totalnum_by_uid($uid, $direction);
		$return['page'] = prepage($page, $limit, $count);
		$sqladd = '';
		//这里出现一个错误，在选取粉丝的时候，取member表也是friendid=uid
		if($direction == 0) { 
			//这里有小问题，只能看粉丝或关注的单向资料
			$sqls = "f.friendid";
			$sqladd = "f.uid='$uid' OR f.friendid='$uid'";
		} elseif($direction == 1) {
			$sqls = "f.friendid";
			$sqladd = "f.uid='$uid' ";
		} elseif($direction == 2) {
			$sqls = "f.uid";
			$sqladd = "f.friendid='$uid' ";
		} elseif($direction == 3) {
			$sqls = "f.friendid";
			$sqladd = "f.uid='$uid' AND f.direction='3'";
		}
		$avatar = $avatar == 0 ? '' : ',m.avatar';
		if($sqladd) {
			$sql = "SELECT f.*, m.username $avatar FROM ".DB::t('u_friends')." f LEFT JOIN ".DB::t('u_members')." m ON $sqls=m.uid WHERE $sqladd ORDER BY version DESC LIMIT {$return['page']['offset']}, $limit";
			$return['data'] = DB::fetch_all($sql);
			return $return;
		} else {
			return array();
		}
	}

	function is_friend($uid, $friendids, $direction = 0) {
		$friendid_str = implode("', '", $friendids);
		$sqladd = '';
		//修复
		if($direction == 0) {
			$sqladd = "uid='$uid' OR friendid='$uid'";
		} elseif($direction == 1) {
			$sqladd = "uid='$uid' AND friendid IN ('$friendid_str') ";
		} elseif($direction == 2) {
			$sqladd = "friendid='$uid' AND uid IN ('$friendid_str') ";
		} elseif($direction == 3) {
			$sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='3'";
		}
		//新增对0的判断
		if(DB::result_first("SELECT COUNT(*) FROM ".DB::t('u_friends')." WHERE $sqladd") == count($friendids) && ($friendids[0] != 0)) {
			return true;
		} else {
			return false;
		}
	}
}
